package com.imagecollector.common;

import com.imagecollector.common.enums.CodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果类
 */
@Data
public class R implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    private R() {
    }

    private R(CodeEnum codeEnum){
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
    }

    private R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static R success(){
        R r = new R();
        r.setCode(CodeEnum.SUCCESS.getCode());
        r.setMsg(CodeEnum.SUCCESS.getMsg());
        return r;
    }

    public static R fail() {
        R r = new R();
        r.setCode(CodeEnum.FAIL.getCode());
        return r;
    }

    public R code(Integer code){
        this.code = code;
        return this;
    }

    public R msg(String msg){
        this.msg = msg;
        return this;
    }

    public static R fail(Integer code, String msg){
        return new R(code, msg);
    }

    public static R fail(CodeEnum codeEnum){
        return new R(codeEnum);
    }

    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
